package com.zut.base.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zut.base.common.entry.Pagination;
import com.zut.base.common.entry.ResultVO;
import com.zut.base.entity.Directory;
import com.zut.base.entity.Project;
import com.zut.base.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 * 项目表 前端控制器
 * </p>
 *
 * @author yyl
 * @since 2021-03-31
 */
@RestController
@RequestMapping("/imp/project")
public class ProjectController {

    @Autowired
    private IProjectService projectServiceImpl;

    @PostMapping("/addProject")
    public Object addProject(@RequestBody JSONObject oj){
        Project pro = new Project();
        pro.setName(oj.getString("name"));
        pro.setBasepath(oj.getString("basepath"));
        pro.setIntroduction(oj.getString("introduction"));
        pro.setEndmonth(oj.getString("endmonth"));
        pro.setEndday(oj.getString("endday"));
        pro.setCreateuserid(oj.getString("createuserid"));
        pro.setDirectoryid(oj.getString("directoryid"));
        List<String> module = (List<String>) oj.get("module");
        List<Directory> list = new ArrayList<>();
        for (String str : module){
            Directory directory = new Directory();
            directory.setName(str);
            directory.setCategory("1");
            directory.setCreateby(oj.getString("createuserid"));
            directory.setUpdateby(oj.getString("createuserid"));
            list.add(directory);
        }
        boolean rs = projectServiceImpl.addProjectAndMoudle(pro, list, oj.getString("createuserid"));
        if(rs) return "创建成功!";
        return new ResultVO<>(-9999,"创建失败！");
    }

    //根据目录id查询项目列表
    @PostMapping("/getProjectListById")
    public Object getProjectListById(@RequestBody JSONObject oj){
        int current = Integer.parseInt(oj.getString("current"));
        int size = Integer.parseInt(oj.getString("size"));
        String userId = oj.getString("userId");
        String type = oj.getString("type");
        Map<String,Object> pro = projectServiceImpl.getProjectListById(current, size, oj.getString("id"), userId, type);
        //格式化时间
        List<HashMap<String,Object>> list = (List<HashMap<String,Object>>)pro.get("list");
        for(Map map : list){
            map.put("createTime",String.valueOf(map.get("createTime")).substring(0,10));
            map.put("progress",Integer.parseInt((String) map.get("progress")));
        }
        return pro;
    }

}
